﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKBatchPrinter.Utils;

namespace YJKBatchPrinter
{
	public partial class PrintMgrForm : System.Windows.Forms.Form
	{
		public PrintMgrForm(PrintMgr printMgr, UIApplication uiRvtApp)
		{
			if (printMgr == null)
			{
				throw new System.ArgumentNullException("printMgr");
			}
			this.m_printMgr = printMgr;
			this.m_uiRvtApp = uiRvtApp;
			this.m_doc = this.m_uiRvtApp.ActiveUIDocument.Document;
			this.InitializeComponent();
		}

		private void setupButton_Click(object sender, EventArgs e)
		{
			this.m_printMgr.ChangePrintSetup();
			this.printSetupNameLabel.Text = this.m_printMgr.PrintSetupName;
		}

		private IList<string> createPrinterComboxDataSource()
		{
			IList<string> list = new List<string>();
			PrintManager rvtDocPrinter = this.m_printMgr.getRvtDocPrinter();
			return PrinterFilter.getPdfAndPltPrinterNames(rvtDocPrinter);
		}

		private bool setPrintSettingByName(string sName)
		{
			bool result = false;
			ICollection<ElementId> printSettingIds = this.m_uiRvtApp.ActiveUIDocument.Document.GetPrintSettingIds();
			try
			{
				foreach (ElementId elementId in printSettingIds)
				{
					Element element = this.m_uiRvtApp.ActiveUIDocument.Document.GetElement(elementId);
					if (element.Name.Equals(sName))
					{
						this.m_printMgr.getRvtDocPrinter().PrintSetup.CurrentPrintSetting = (element as PrintSetting);
						result = true;
						break;
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = false;
			}
			return result;
		}

		private List<string> GetCurDocAllPrintSetting()
		{
			List<string> list = new List<string>();
			ICollection<ElementId> printSettingIds = this.m_uiRvtApp.ActiveUIDocument.Document.GetPrintSettingIds();
			foreach (ElementId elementId in printSettingIds)
			{
				Element element = this.m_uiRvtApp.ActiveUIDocument.Document.GetElement(elementId);
				PrintSetting printSetting = element as PrintSetting;
				if (printSetting != null && printSetting.PrintParameters != null)
				{
					list.Add(element.Name);
				}
			}
			return list;
		}

		private bool createDefaultPrintSetting(string sName)
		{
			bool result = false;
			bool flag = false;
			ICollection<ElementId> printSettingIds = this.m_uiRvtApp.ActiveUIDocument.Document.GetPrintSettingIds();
			foreach (ElementId elementId in printSettingIds)
			{
				Element element = this.m_uiRvtApp.ActiveUIDocument.Document.GetElement(elementId);
				if (element.Name.Equals(sName))
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
                this.m_printMgr.getRvtDocPrinter().PrintSetup.CurrentPrintSetting.PrintParameters.ZoomType = (ZoomType)1;
				this.m_printMgr.getRvtDocPrinter().PrintSetup.CurrentPrintSetting.PrintParameters.Zoom = 98;
				this.m_printMgr.getRvtDocPrinter().PrintSetup.CurrentPrintSetting.PrintParameters.PaperPlacement = 0;
                this.m_printMgr.getRvtDocPrinter().PrintSetup.CurrentPrintSetting.PrintParameters.PageOrientation = (PageOrientationType)1;
				result = this.m_printMgr.getRvtDocPrinter().PrintSetup.SaveAs(sName);
			}
			return result;
		}

		private void PrintMgrForm_Load(object sender, EventArgs e)
		{
			this.printerNameComboBox.DataSource = this.createPrinterComboxDataSource();
			this.printerNameComboBox.SelectedValueChanged += this.printerNameComboBox_SelectedValueChanged;
			this.printerNameComboBox.SelectedItem = this.m_printMgr.PrinterName;
			if (this.m_printMgr.VerifyPrintToFile(this.printToFileCheckBox))
			{
				this.printToFileCheckBox.Checked = this.m_printMgr.IsPrintToFile;
			}
			Collection<System.Windows.Forms.Control> collection = new Collection<System.Windows.Forms.Control>();
			collection.Add(this.copiesNumericUpDown);
			collection.Add(this.numberofcoyiesLabel);
			this.m_printMgr.VerifyCopies(collection);
			collection.Clear();
			collection.Add(this.printToFileNameLabel);
			collection.Add(this.printToFileNameTextBox);
			collection.Add(this.browseButton);
			this.m_printMgr.VerifyPrintToFileName(collection);
			this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton);
			if (this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton))
			{
				this.singleFileRadioButton.Checked = this.m_printMgr.IsCombinedFile;
				this.separateFileRadioButton.Checked = !this.m_printMgr.IsCombinedFile;
			}
			if (!this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton) && this.m_printMgr.VerifyPrintToSeparateFile(this.separateFileRadioButton))
			{
				this.separateFileRadioButton.Checked = true;
			}
			this.singleFileRadioButton.CheckedChanged += this.combineRadioButton_CheckedChanged;
			this.currentWindowRadioButton.CheckedChanged += this.currentWindowRadioButton_CheckedChanged;
			this.visiblePortionRadioButton.CheckedChanged += this.visiblePortionRadioButton_CheckedChanged;
			this.selectedViewsRadioButton.CheckedChanged += this.selectedViewsRadioButton_CheckedChanged;
            switch ((int)this.m_printMgr.PrintRange)
			{
			case 0:
				this.currentWindowRadioButton.Checked = true;
				break;
			case 1:
				this.visiblePortionRadioButton.Checked = true;
				break;
			case 2:
				this.selectedViewsRadioButton.Checked = true;
				break;
			}
			if (!string.IsNullOrEmpty(this.m_printMgr.PrintToFileName))
			{
				this.printToFileNameTextBox.Text = this.m_printMgr.PrintToFileName;
			}
			else
			{
				this.printToFileNameTextBox.Text = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "\\" + this.m_printMgr.DocumentTitle;
			}
			collection.Clear();
			collection.Add(this.selectedViewSheetSetLabel);
			collection.Add(this.selectedViewSheetSetButton);
			if (this.m_printMgr.VerifySelectViewSheetSet(collection))
			{
				this.selectedViewSheetSetLabel.Text = this.m_printMgr.SelectedViewSheetSetName;
			}
			this.orderCheckBox.Checked = this.m_printMgr.PrintOrderReverse;
			this.orderCheckBox.CheckedChanged += this.orderCheckBox_CheckedChanged;
			if (this.m_printMgr.VerifyCollate(this.collateCheckBox))
			{
				this.collateCheckBox.Checked = this.m_printMgr.Collate;
			}
			this.collateCheckBox.CheckedChanged += this.collateCheckBox_CheckedChanged;
			this.setPrintSettingByName("HYPrintSetting");
			string tempPath = Path.GetTempPath();
			string sFullPath = tempPath + "PrintSetupForm.ini";
			FormDataIni formDataIni = new FormDataIni(sFullPath);
			string text = formDataIni.ReadData("UIData", "打印设置名称", "");
			if (!string.IsNullOrEmpty(text) && PrintSettingManager.IsPrintSettingValid(text, this.m_doc))
			{
				this.printSetupNameLabel.Text = text;
				return;
			}
			this.printSetupNameLabel.Text = this.m_printMgr.PrintSetupName;
		}

		private void printerNameComboBox_SelectedValueChanged(object sender, EventArgs e)
		{
			this.m_printMgr.PrinterName = (this.printerNameComboBox.SelectedItem as string);
			this.m_printMgr.VerifyPrintToFile(this.printToFileCheckBox);
			Collection<System.Windows.Forms.Control> collection = new Collection<System.Windows.Forms.Control>();
			collection.Add(this.copiesNumericUpDown);
			collection.Add(this.numberofcoyiesLabel);
			this.m_printMgr.VerifyCopies(collection);
			collection.Clear();
			collection.Add(this.printToFileNameLabel);
			collection.Add(this.printToFileNameTextBox);
			collection.Add(this.browseButton);
			if (!string.IsNullOrEmpty(this.printToFileNameTextBox.Text))
			{
				this.printToFileNameTextBox.Text = this.printToFileNameTextBox.Text.Remove(this.printToFileNameTextBox.Text.LastIndexOf(".")) + this.m_printMgr.PostFix;
			}
			this.m_printMgr.VerifyPrintToFileName(collection);
			this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton);
			this.m_printMgr.VerifyPrintToSeparateFile(this.separateFileRadioButton);
		}

		private void printToFileCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			this.m_printMgr.IsPrintToFile = this.printToFileCheckBox.Checked;
			Collection<System.Windows.Forms.Control> collection = new Collection<System.Windows.Forms.Control>();
			collection.Add(this.copiesNumericUpDown);
			collection.Add(this.numberofcoyiesLabel);
			this.m_printMgr.VerifyCopies(collection);
			collection.Clear();
			collection.Add(this.printToFileNameLabel);
			collection.Add(this.printToFileNameTextBox);
			collection.Add(this.browseButton);
			this.m_printMgr.VerifyPrintToFileName(collection);
			this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton);
		}

		private void combineRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			if (this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton))
			{
				this.m_printMgr.IsCombinedFile = this.singleFileRadioButton.Checked;
			}
		}

		private void browseButton_Click(object sender, EventArgs e)
		{
			string text = this.m_printMgr.ChangePrintToFileName();
			if (!string.IsNullOrEmpty(text))
			{
				this.printToFileNameTextBox.Text = text;
			}
		}

		private void currentWindowRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			if (this.currentWindowRadioButton.Checked)
			{
				this.m_printMgr.PrintRange = 0;
				Collection<System.Windows.Forms.Control> collection = new Collection<System.Windows.Forms.Control>();
				collection.Add(this.selectedViewSheetSetLabel);
				collection.Add(this.selectedViewSheetSetButton);
				this.m_printMgr.VerifySelectViewSheetSet(collection);
				if (this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton))
				{
					this.m_printMgr.IsCombinedFile = true;
					this.singleFileRadioButton.Checked = true;
					this.separateFileRadioButton.Checked = false;
				}
				this.m_printMgr.VerifyPrintToSeparateFile(this.separateFileRadioButton);
				this.m_printMgr.VerifyCollate(this.collateCheckBox);
				this.buttonPreView.Enabled = this.currentWindowRadioButton.Checked;
			}
		}

		private void visiblePortionRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			if (this.visiblePortionRadioButton.Checked)
			{
                this.m_printMgr.PrintRange = (PrintRange)1;
				Collection<System.Windows.Forms.Control> collection = new Collection<System.Windows.Forms.Control>();
				collection.Add(this.selectedViewSheetSetLabel);
				collection.Add(this.selectedViewSheetSetButton);
				this.m_printMgr.VerifySelectViewSheetSet(collection);
				if (this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton))
				{
					this.m_printMgr.IsCombinedFile = true;
					this.singleFileRadioButton.Checked = true;
					this.separateFileRadioButton.Checked = false;
				}
				this.m_printMgr.VerifyPrintToSeparateFile(this.separateFileRadioButton);
				this.m_printMgr.VerifyCollate(this.collateCheckBox);
				this.buttonPreView.Enabled = this.visiblePortionRadioButton.Checked;
			}
		}

		private void selectedViewsRadioButton_CheckedChanged(object sender, EventArgs e)
		{
			if (this.selectedViewsRadioButton.Checked)
			{
                this.m_printMgr.PrintRange = (PrintRange)2;
				Collection<System.Windows.Forms.Control> collection = new Collection<System.Windows.Forms.Control>();
				collection.Add(this.selectedViewSheetSetLabel);
				collection.Add(this.selectedViewSheetSetButton);
				this.m_printMgr.VerifySelectViewSheetSet(collection);
				this.m_printMgr.VerifyPrintToSingleFile(this.singleFileRadioButton);
				if (this.m_printMgr.VerifyPrintToSeparateFile(this.separateFileRadioButton))
				{
					this.separateFileRadioButton.Checked = true;
				}
				this.m_printMgr.VerifyPrintToSeparateFile(this.separateFileRadioButton);
				this.m_printMgr.VerifyCollate(this.collateCheckBox);
				this.buttonPreView.Enabled = !this.selectedViewsRadioButton.Checked;
			}
		}

		private void orderCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			this.m_printMgr.PrintOrderReverse = this.orderCheckBox.Checked;
		}

		private void collateCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			this.m_printMgr.Collate = this.collateCheckBox.Checked;
		}

		private void selectButton_Click(object sender, EventArgs e)
		{
			this.m_printMgr.SelectViewSheetSet();
			this.selectedViewSheetSetLabel.Text = this.m_printMgr.SelectedViewSheetSetName;
		}

		private void copiesNumericUpDown_ValueChanged(object sender, EventArgs e)
		{
			try
			{
				this.m_printMgr.CopyNumber = (int)this.copiesNumericUpDown.Value;
			}
			catch (InvalidOperationException)
			{
				this.collateCheckBox.Enabled = false;
				return;
			}
			this.m_printMgr.VerifyCollate(this.collateCheckBox);
		}

		private void okButton_Click(object sender, EventArgs e)
		{
			bool flag = true;
			try
			{
                if ((int)this.m_printMgr.getRvtDocPrinter().IsVirtual == 1)
				{
					if (this.printSetupNameLabel.Text.Contains("在任务中"))
					{
						MessageBox.Show("打印设置为<在任务中>, 请重新配置打印设置");
						return;
					}
					if (this.currentWindowRadioButton.Checked || this.visiblePortionRadioButton.Checked)
					{
						flag = this.m_printMgr.printViewWithSpecifySize(printType.PT_OneView);
					}
					else if (this.selectedViewsRadioButton.Checked)
					{
						if (this.singleFileRadioButton.Checked)
						{
							flag = this.m_printMgr.printViewWithSpecifySize(printType.PT_ViewOneFile);
						}
						else
						{
							flag = this.m_printMgr.printViewWithSpecifySize(printType.PT_ViewsFiles);
						}
					}
				}
				else
				{
					flag = this.m_printMgr.SubmitPrint();
				}
				if (flag)
				{
					base.Close();
					base.DialogResult = DialogResult.OK;
				}
			}
			catch (Exception)
			{
				this.m_doc.Regenerate();
				PrintMgr.MyMessageBox("打印失败，请更换虚拟打印机。");
			}
		}

		private void helpButton_Click_1(object sender, EventArgs e)
		{
			string sWeb = "http://help.autodesk.com/view/RVT/2015/CHS/?guid=GUID-2E9ACAF3-4563-4820-8E33-1E70ACC0FB00";
			PrinterFilter.openWebWithdefaultScanning(sWeb);
		}

		private void buttonPreView_Click(object sender, EventArgs e)
		{
			RevitCommandId revitCommandId = RevitCommandId.LookupPostableCommandId(Autodesk.Revit.UI.PostableCommand.PrintPreview);
			string name = revitCommandId.Name;
			try
			{
				this.m_uiRvtApp.PostCommand(revitCommandId);
			}
			catch (Exception ex)
			{
				TaskDialog.Show("error", ex.Message);
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.Close();
			}
			return false;
		}

		protected override void OnLoad(EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			base.OnLoad(e);
		}

		private PrintMgr m_printMgr;

		private UIApplication m_uiRvtApp;

		private Document m_doc;
	}
}
